/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-11-28
 * Time: 11:40
 */
public class test1 {
    /*
    NewCoder BM10 两个链表的第一个公共结点
    输入两个无环的单向链表，找出它们的第一个公共结点，如果没有公共节点则返回空。
     */
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode cur1 = pHead1;
        ListNode cur2 = pHead2;
        while (cur1 != cur2) {
            cur1 = (cur1 != null ? cur1.next : pHead2);
            cur2 = (cur2 != null ? cur2.next : pHead1);
        }
        return cur1;
    }

    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
